package progark.a9.javaserver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class PlayerManagement {
	
	private static PlayerManagement instance = null;
	
	private static Map<String, Player> players;
	private static ArrayList<String> turns;
	private static Player currentPlayer;
	private static int turn = 0;
	private static boolean gameOver;
	private static Player winner;
	
	private PlayerManagement(){
		players = new HashMap<String, Player>();
		turns = new ArrayList<String>();
		currentPlayer = new Player();
		winner = null;
		gameOver = false;
	}
	
	public static synchronized PlayerManagement getInstance(){
		if(instance == null)
			instance = new PlayerManagement();
		return instance;
	}
	
	public static synchronized String addPlayer(){
		Player player = new Player();
		player.setId("Player" + (players.size() + 1));
		players.put(player.getId(), player);
		turns.add(player.getId());
		currentPlayer.setId(turns.get(0));
		return player.getId();
	}
	
	public static synchronized Map<String, Player> getPlayers(){
		return players;
	}
	
	public static synchronized Player getCurrentPlayer(){
		return currentPlayer;
	}
	
	public static synchronized void nextTurn(){
		turn++;
		if(turn >= turns.size())
			turn = 0;
		currentPlayer.setId(turns.get(turn));
	}
	
	public static synchronized boolean getGameOver(){
		return gameOver;
	}
	
	public static synchronized void setGameOver(){
		gameOver = true;
	}
	
	public static synchronized Player getWinner(){
		return winner;
	}
	
	public static synchronized void setWinner(String player){
		if(winner == null)
			winner = players.get(player);
	}

}
